<?php
class Banner extends Controller
{
	private  $_template_f; 
	private $_numShowItem = 10;
	private $_lang;
	function __construct()
	{
		parent::Controller();
		$this->_lang = $this->mycommon->getLangCode();
		$this->load->language('common', $this->_lang);
		$common_lang = $this->lang->line('common_lang');
		if(!$this->session->userdata('uname'))
		{
			redirect(site_url('login/'));
			die();
		}
		else
		{
			$permitid = $this->session->userdata('permitid');
			if(!is_numeric($permitid) || $permitid > -1)
			{
				show_error($common_lang['access_denied']);
				die();
			}
		}
		$this->_template_f = $this->config->item('template_f');
		$this->load->language('banner/banner', $this->_lang);
		//$this->load->language('left_menu', $this->_lang);
		$this->load->helper('breadcrumb');
        $this->load->model('banner/banner_model');
        $this->load->model('campaign/campaign_model');
        $this->load->library('mychart');
	}
	
	function _loadHeader($title = '')
	{
		$header = array();
		$header['title'] = $title == '' ? 'Self serving' : $title;
		$header['template_f'] = $this->_template_f;
		
		$header['username'] = '';
		$header['userid'] = '';
		$header['permitid'] = '';
		$header['module_name'] = 'banner';
		$header['lang'] = $this->_lang;
		$header['common_lang'] = $this->lang->line('common_lang');
        
		if($this->session->userdata('uname'))
		{
			$header['username'] = $this->session->userdata('uname');
			$header['userid'] = $this->session->userdata('uid');
			$header['permitid'] = $this->session->userdata('permitid');
		}
		
		 //load header
		$this->load->view($this->_template_f . 'preheader_view');
		$this->load->view($this->_template_f . 'header_view', $header);
	}
	
	function _loadMenu()
	{
		$data = array();
		$data['username'] = '';
		$data['userid'] = '';
		$data['permitid'] = '';
		$data['lang'] = $this->_lang;
		if($this->session->userdata('uname'))
		{
			$data['username'] = $this->session->userdata('uname');
			$data['userid'] = $this->session->userdata('uid');
			$data['permitid'] = $this->session->userdata('permitid');
		}
		$data['common_lang'] = $this->lang->line('common_lang');
        
		$this->load->view($this->_template_f . 'menu_view',$data);
	}
	
	function _loadFooter()
	{
		$this->load->view($this->_template_f . 'footer_view');
	}
    
    function index()
    {
    	$fromDate = $this->input->get('fromdate');
		$fromDate = ($fromDate == '') ? date('Y-m-d', strtotime ( '-7 day' , strtotime (date('Y-m-d')))) : $fromDate;
		
		$todate = $this->input->get('todate');
		$todate = ($todate == '') ? date('Y-m-d') : $todate;
		
		$status = $this->input->get('status');
		$status = $status == '' ? '123' : $status;
		$groupid = $this->input->get('group');
		$groupid = is_numeric($groupid) && $groupid != '' ? $groupid :-1;
		$strUserId = $this->input->get('user', true);

		$cPage = $this->input->get('cpage', true);
		$cPage = is_numeric($cPage) && $cPage != '' ? $cPage : 1;	
		$numShowItem = $this->input->get('numitem', true);
		$numShowItem = is_numeric($numShowItem) && $numShowItem != '' ? $numShowItem : $this->_numShowItem;
		
        //load header
  		$this->_loadHeader('Self Serving - All banner');
  		//load menu
        $this->_loadMenu();
		
  		$data = array();
        $data['fromdate']= date('d-m-Y', strtotime($fromDate)); 
        $data['todate']= date('d-m-Y', strtotime($todate));
        
        $data['fdateUrl']= $fromDate; 
        $data['tdateUrl']= $todate;
        
        $data['status'] = $status;
        $data['groupid'] = $groupid;
        $data['strUserId'] = $strUserId;
        $data['cPage'] = $cPage;
        $data['numShowItem'] = $numShowItem;
        
        $data['userid'] = $userid = $this->session->userdata('uid');
        $data['permitID'] = $permitID = $this->session->userdata('permitid');
        
        $userid=$this->session->userdata('uid');
        
        $data['lstUser'] = array();
        if($strUserId != '')
        {
        	$data['lstUser'] = $this->campaign_model->get_user_by_listid($strUserId);
        }
		$temp = $this->banner_model->selfserving_get_all_banners_by_user($status, $fromDate, $todate, $userid, $cPage, $numShowItem, $strUserId,$groupid);
        $data['info'] = $temp['info'];
		$data['iCount'] = $temp['iCount'];
		$data['pCount'] = $temp['pCount'];    
		$data['lstGroup'] = $this->campaign_model->get_all_group();
		
        $data['banner_lang']=$this->lang->line('banner_lang');
  		$this->load->view($this->_template_f.'banner/banner_view',$data);
  		$this->_loadFooter();
    }
    function filter()
    {
        $data['page'] = $page = $this->input->get('page') ? $this->input->get('page') : '1';
        $data['numrow'] = $numrow = $this->input->get('numrow') ? $this->input->get('numrow') : '20';
        
        $status = $this->input->get('status');
    	$data['fromdate'] = $fromdate = date('Y-m-d',strtotime($this->input->get('fromdate')));
        $data['todate'] = $todate = date('Y-m-d',strtotime($this->input->get('todate')));
        $userid=$this->session->userdata('uid');
        
        $temp = $this->banner_model->selfserving_get_all_banners_by_user($status,$fromdate,$todate,$userid,$page,$numrow);
        $data['info'] = $temp['info'];
        $data['totalpage'] = $temp['total'][0]['totalpage'];     
        $data['banner_lang']=$this->lang->line('banner_lang');
		$this->load->view($this->_template_f.'banner/table_view',$data);
    }
    function edit()
    {
        $val = $this->input->post('ns') ? $this->input->post('ns') : '123';
        $type = $this->input->get('type');
        if($val==4)
        {
            //restore
            $type=4;
            $val=0;
        }elseif($val==3)
        {
            //deleted
            $type=4;
            $val=1;
        }
        $bannerid = $this->input->get('bannerid');       
        $count = $this->banner_model->selfserving_banner_update_inline($bannerid,$val,$type);
        if($count==true)
        {
            if($type==1 && $val=='1'){
                echo '<img class="cursor-pointer" alt="" src="/images/imgrun.gif"/>';
            }elseif($type==1 && $val=='2')
            {
                echo '<img class="cursor-pointer" alt="" src="/images/imgpause.gif"/>';
            }elseif($type==4 && $val=='1')
            {
                echo '<img class="cursor-pointer" alt="" src="/images/temp_deleted.gif"/>';
                echo '<script type="text/javascript">window.location.reload();</script>';
            }elseif($type==4 && $val=='0')
            {
                echo '<img class="cursor-pointer" alt="" src="/images/temp_deleted.gif"/>';
                echo '<script type="text/javascript">window.location.reload();</script>';
            }else{
                echo $val;
            }
        }else{
            echo 'Error';
        }      
    }
    function editSpeed()
    {
        $val = $this->input->get('val');
        $bannerid = $this->input->get('bannerid');
        $type = $this->input->get('type');
        $default= $this->input->get('default');
        $count = $this->banner_model->selfserving_banner_update_inline($bannerid,$val,$type);
        if($count==true)
        {
            echo $val;
        }else{
            echo 'Error,try again,please!';
        }      
    }
    function load_tooltip()
    {
        $data['bannerid'] = $bannerid = $this->input->get('bannerid');
        $data['index'] = $this->input->get('index');
  		if(!isset($todate))
        {
            $data['todate']=$todate=date('Y-m-d');
            $fromdate = strtotime ( '-7 day' , strtotime ( $todate ) ) ;
            $data['fromdate']=$fromdate = date ( 'Y-m-d' , $fromdate );
        }
        $userid=$this->session->userdata('uid');
        $data['status']=$status=$this->input->get('status');
        $this->load->model('bannerdetail/bannerdetail_model');
        $bannerdetailinfoTemp=$this->bannerdetail_model->selfserving_get_banner_info($bannerid);
        $data['info']=$bannerdetailinfoTemp['info'];
        $data['category']=$bannerdetailinfoTemp['category'];
        $data['banner_lang']=$this->lang->line('banner_lang');
        $this->load->view($this->_template_f . 'banner/tooltip',$data);
    }
    function domain_report_tooltip()
    {
        $bannerid = $this->input->get('bannerid');
        $typeid = $this->input->get('typeid');
        $data['typedatata']=$typedatata = $this->input->get('typedatata');
        $fromdate = date('Y/m/d',strtotime($this->input->get('fromdate')));
        $todate = date('Y/m/d',strtotime($this->input->get('todate')));
        $status = $this->input->get('status');
        $data['info']=$this->banner_model->selfserving_get_popupdomain($bannerid,$typeid,$typedatata,$fromdate,$todate,$status);
        $data['banner_lang']=$this->lang->line('banner_lang');
        $this->load->view($this->_template_f . 'banner/domain_report_tooltip',$data);
    }
    function search()
    {
        $banner_lang = $this->lang->line('banner_lang');
        //load header
        $data['page'] = $page = (is_numeric($this->input->get('page'))&&($this->input->get('page')>0)) ? $this->input->get('page') : '1';
        $data['numrow'] = $numrow = (is_numeric($this->input->get('numrow'))&&($this->input->get('numrow')>0)) ? $this->input->get('numrow') : '20';
        
  		$this->_loadHeader('');
        $this->_loadMenu();
  		//load menu
        if(!isset($todate))
        {
            $data['todate']=$todate=date('Y-m-d');
            $data['fromdate'] = $fromdate = date ( 'Y-m-d' , strtotime ( '-180 day' , strtotime ( $todate ) ) );
            $data['fromdate1'] = date ( 'Y-m-d' , strtotime ( '-7 day' , strtotime ( $todate ) ) );
        }
        $userid=$this->session->userdata('uid');
        $data['name'] = $name =trim($this->input->get('name'));
        $temp = $this->banner_model->selfserving_search_banners($fromdate,$todate,$userid,$name,$page,$numrow);
        $data['info'] = $temp['data'];
        $data['totalpage'] = $temp['totalpage'][0]['totalpage'];
        $data['banner_lang']=$banner_lang;
        $this->load->view($this->_template_f . 'banner/banner_search_view',$data);
        //load footer
        $this->_loadFooter();
    }
    function deleteAd($bannerid)
    {
        if(!$this->session->userdata('uname'))
		{
			$currUrl = $this->mycommon->getCurrentUrl();
			redirect(site_url('/login'). '/?url=' . urlencode($currUrl));
			die();
		}
        $type=4;
        $val=1;
        
        $user_id = $this->session->userdata('uid');
		$group_id = $this->session->userdata('groupid');
		$permit_id = $this->session->userdata('permitid');
        $chkBanner = check_permit_id($user_id,$permit_id,$group_id,$bannerid,1);
        if($chkBanner==false)
        {
            redirect(site_url('banner/error'));
            die();
        }
        $count = $this->banner_model->selfserving_banner_update_inline($bannerid,$val,$type);
        if($count==true)
        {
            redirect(site_url('banner'));
        }else{
            redirect(site_url('banner/error'));
        }      
    }
}
?>